package com.chapter2.basics;

import java.util.Random;


/**
 * rewrite the INSETTION_SORT procedure to sort into decreasing order
 * 
 * @author Administrator
 * 
 */
public class Exercises_2_1_2 {
	public static void main(String[] args) {
		Random rand = new Random(100);
		
		int n = 100;
		int[] A = new int[n];
		//产生长度为n的数组
		for(int k =0 ; k<n; k++){
			A[k] = rand.nextInt(1000);
		}
		for (int j = 1; j < n; j++) {
			int key = A[j];
			int i = j - 1;
			//如果A[i]<key,A[i]对应的值后移，i=  i-1；
			while (i > 0 && A[i] < key) {
				
				A[i + 1] = A[i];
				i--;
			}
			//如果不满足上述循环插入key值
			A[i+1] = key;
		}
		for(int i = 0 ;i<n; i++ )
		System.out.println(A[i]);
	}

}
